package com.codescan.admin.modules.codescan.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.codescan.admin.common.api.CommonResult;
import com.codescan.admin.modules.codescan.dto.SourceReportDTO;
import com.codescan.admin.modules.codescan.model.SourceReportVo;
import com.codescan.admin.modules.codescan.service.ISourceReportService;
import com.codescan.admin.modules.codescan.vo.Results;
import com.codescan.admin.modules.codescan.vo.SourceReport;
import com.codescan.admin.modules.codescan.vo.XssScan;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.io.*;
import java.util.*;

@Api(tags = "SourceReportController", description = "源码分析报告")
@RestController
@RequestMapping("/sourcereport")
@Slf4j
public class SourceReportController {
    @Autowired
    private ISourceReportService sourceReportService;

    @PostMapping("/list")
    public CommonResult<?> list(@RequestBody(required = false) SourceReportDTO dto){
        Page page = new Page<>(1,10);
        SourceReportVo sourceReportVo = new SourceReportVo();
        if(Objects.nonNull(dto)){
            BeanUtils.copyProperties(dto,sourceReportVo);
        }
        QueryWrapper<SourceReportVo> wrapper = new QueryWrapper<>(sourceReportVo);
        IPage<SourceReportVo> list = sourceReportService.page(page,wrapper);
        return CommonResult.success(list);
    }

    @PostMapping("/save")
    public CommonResult<?> save(@RequestBody SourceReportVo sourceReportVo){
        boolean flag = sourceReportService.saveReport(sourceReportVo);
        return CommonResult.success(flag);
    }

    @GetMapping("/delete")
    public CommonResult<?> delete(@RequestParam String id ){
        boolean flag = sourceReportService.removeById(id);
        return CommonResult.success(flag);
    }

    @GetMapping("/fenxi")
    public CommonResult<?> fenxi() {
        String jsonStr = "";
        String url = "/Users/gxuehe/workspace/personal/secdec-xssmap/sample-json/xssmap_test_GET_output.json";
        try{
        File jsonFile = new File(url);
        FileReader fileReader = new FileReader(jsonFile);
        Reader reader = new InputStreamReader(new FileInputStream(jsonFile), "utf-8");
            int ch = 0;
            StringBuffer sb = new StringBuffer();
            while ((ch = reader.read()) != -1) {
                sb.append((char) ch);
            }
            fileReader.close();
            reader.close();
            jsonStr = sb.toString();
            SourceReport sourceReport = JSONObject.parseObject(jsonStr, SourceReport.class);
            sourceReport.setTargetUrl("xxxx");
            sourceReportService.createPdf(sourceReport);
        }catch (Exception e){
            log.error("解析报告失败！");
            return CommonResult.failed("解析报告失败");
        }
        return CommonResult.success(jsonStr);
    }
}
